//
//  MyDocument.m
//  Shopping List
//
//  Created by Tim Isted on 16/09/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MyDocument.h"

@implementation MyDocument

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
	return [shoppingListArray count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
	return [shoppingListArray objectAtIndex:rowIndex];
}

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
	[shoppingListArray replaceObjectAtIndex:rowIndex withObject:anObject];
}

- (IBAction)addNewItemToShoppingList:(id)sender
{
	NSString *newItem = [newItemNameTextField stringValue]; 
	
	NSString *itemToAdd = [newItemNameTextField stringValue];
	for( NSString *eachItem in shoppingListArray )
	{
		if( [eachItem isEqualToString:itemToAdd] )
		{
			NSAlert *alert = [[NSAlert alloc] init];
			
			[alert addButtonWithTitle:@"Duplicate"];
			[alert addButtonWithTitle:@"Cancel"];
			
			[alert setMessageText:@"This item already exists in your shopping list."];
			[alert setInformativeText:@"Do you really want to add a duplicate item?"];
			[alert setAlertStyle:NSWarningAlertStyle];
			
			int returnValue = [alert runModal]; 
			[alert release];
			if( returnValue != NSAlertFirstButtonReturn )
				return;
			else 
				break;
		}
	}
	
	[shoppingListArray addObject:newItem];
	
	if( sender == newItemNameTextField )
		[newItemNameTextField setStringValue:@""];
	
	[shoppingListTableView reloadData];
}

- (IBAction)removeItemFromShoppingList:(id)sender
{
	int selectedItemIndex = [shoppingListTableView selectedRow];
	if( selectedItemIndex == -1 ) return;
	
	NSAlert *alert = [[NSAlert alloc] init]; 
	[alert addButtonWithTitle:@"Delete"]; 
	[alert addButtonWithTitle:@"Cancel"]; 
	[alert setMessageText:@"Delete the shopping list item?"]; 
	[alert setInformativeText:@"Deleted items cannot be restored."]; 
	[alert setAlertStyle:NSWarningAlertStyle];
	
	int returnValue = [alert runModal]; 
	
	if( returnValue == NSAlertFirstButtonReturn )
	{
		[shoppingListArray removeObjectAtIndex:selectedItemIndex];
		[shoppingListTableView reloadData];
	}
	
	[alert release];
}

- (id)init
{
    self = [super init];
    if (self) {
    
        shoppingListArray = [[NSMutableArray alloc] 
							 initWithObjects:@"Milk", @"Eggs", 
											 @"Butter", nil];    
    }
    return self;
}

- (void)dealloc
{
	[shoppingListArray release]; 
	
	[super dealloc];
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
	 return [shoppingListArray writeToURL:absoluteURL atomically:YES];
}

- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
	[shoppingListArray release];
	shoppingListArray = [[NSMutableArray alloc] initWithContentsOfURL:absoluteURL];
	[shoppingListTableView reloadData];
	
	return YES;
}

@end
